import loadjs from 'loadjs';

const RSAEcrypt = (openRSA, param) => {
    return new Promise((resolve, reject) => {
        //异步加载ras文件
        loadjs.isDefined('rsa') ? loadjs.ready('rsa', () => {
            if (openRSA && openRSA == '1') {
                Object.keys(param).forEach(key => {
                    param[key] = __RSAEcrypt__.rsa_data_encrypt(param[key]);
                });
            }
            resolve(param);
        }) : loadjs(["/js/rsa/jsencrypt.js", "/js/rsa/rsa.js"], 'rsa', {
            async: false,
            success: () => {
                if (openRSA && openRSA == '1') {
                    const callback = () => {
                        Object.keys(param).forEach(key => {
                            param[key] = __RSAEcrypt__.rsa_data_encrypt(param[key]);
                        });
                        resolve(param);
                    }
                    __RSAEcrypt__.initRsaCode(callback);
                } else {
                    resolve(param);
                }
            }
        });
    })
}

export {
    RSAEcrypt
}